from sqlalchemy.ext.asyncio import create_async_engine, AsyncSession
from sqlalchemy.orm import declarative_base, sessionmaker

# 配置SQLite数据库（生产环境建议用PostgreSQL）
DATABASE_URL = "sqlite+aiosqlite:///./test5.db"

# 创建异步引擎
engine = create_async_engine(DATABASE_URL, echo=True)

# 会话工厂
AsyncSessionLocal = sessionmaker(
    bind=engine,
    class_=AsyncSession,
    expire_on_commit=False
)

# 声明基类
Base = declarative_base()

# 依赖项（用于FastAPI路由）
async def get_db():
    async with AsyncSessionLocal() as session:
        yield session